<?php

$db = new SQLiteDatabase('InstantInput.db');

$result = $db->query("SELECT id FROM User_Classes WHERE sunet_id = '" . $_ENV["WEBAUTH_USER"] . "' AND class_id = " . $_GET['uniqueCourseNumber'] . "");
if ($result->numRows() == 0) 
{
	$db->queryExec("Insert INTO User_Classes (sunet_id, class_id) VALUES ('" . $_ENV["WEBAUTH_USER"] . "', " . $_GET['uniqueCourseNumber'] . ")");
}

$classes = array();

$result = $db->query("SELECT * FROM Classes WHERE Classes.class_id IN (SELECT User_Classes.class_id FROM User_Classes WHERE User_Classes.sunet_id = '" . $_ENV["WEBAUTH_USER"] . "')");

while ($result->valid()) 
{
	$curr = $result->current();

	$class = array(
		"uniqueCourseNumber"=> $curr['class_id'],
		"course"=>$curr['course'],
		"title"=>$curr['title'],
		"startTime"=>$curr['start_time'],
		"endTime"=>$curr['end_time'],
		"days"=>array("F"),
		"location"=>$curr['location']
	);
	
	$classes[] = $class;
	$result->next();	
}	

print json_encode($classes);

?>